﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hpdi.VssLogicalLib;

namespace Hpdi.Vss2AccuRev
{
    class SQLExporter : Worker
    {
        RevisionAnalyzer revisionAnalyzer;

        public SQLExporter(WorkQueue workQueue, Logger logger, RevisionAnalyzer revisionAnalyzer)
            :base(workQueue, logger)
        {
            this.revisionAnalyzer = revisionAnalyzer;
        }

        public void ExportRevisions()
        {
            workQueue.AddLast(delegate(object work)
            {
                logger.WriteSectionSeparator();
                LogStatus(work, "Exporting Revisions");

                foreach (var dateEntry in revisionAnalyzer.SortedRevisions)
                {
                    foreach (var revision in dateEntry.Value)
                    {
                        // determine target of project revisions
                        var actionType = revision.Action.Type;
                        var namedAction = revision.Action as VssNamedAction;
                        var targetFile = revision.Item.PhysicalName;
                        if (namedAction != null)
                        {
                            targetFile = namedAction.Name.PhysicalName;
                        }

                        // Create actions are only used to obtain initial item comments;
                        // items are actually created when added to a project
                        var creating = (actionType == VssActionType.Create ||
                            (actionType == VssActionType.Branch && !revision.Item.IsProject));

                        // Share actions are never conflict (which is important,
                        // since Share always precedes Branch)
                        var nonconflicting = creating || (actionType == VssActionType.Share);

                        // look up the pending change for user of this revision
                        // and flush changes past time threshold
                        var pendingUser = revision.User;

                        string actionTarget = revision.Item.LogicalName;
                        int revNum = revision.Version;
                        bool isProject = revision.Item.IsProject;
                        switch (revision.Action.Type)
                        {
                            case VssActionType.Add:
                            case VssActionType.Delete:
                            case VssActionType.Destroy:
                                revNum = 0;
                                actionTarget += '/' + ((Hpdi.VssLogicalLib.VssNamedAction)revision.Action).Name.LogicalName;
                                break;
                        }

                        logger.WriteLine("INSERT INTO File() VALUES({0},{1},{2},{3},{4},{5},{6})",
                            revision.DateTime, actionTarget, revision.Item.IsProject, revision.Version, revision.User, revision.Action.Type, revision.Comment);
                    }
                }
            });
        }
    }
}
